<template>
  <div id="container">
    <el-row>
      <el-col :span="12"
        ><article-list
          :items="auditTitleList"
          :haveMore="haveMore"
          @showMore="showMore"
      /></el-col>
      <el-col :span="12">
        <article-reader :auditContent="auditContent"
      /></el-col>
    </el-row>
  </div>
</template>

<script>
import articleReader from "@/views/article/component/articleReader.vue";
import articleList from "@/views/article/component/articleList.vue";
import {
  getAuditTitleList,
  getAUditArticle,
  passAudit,
  noPassAudit,
} from "@/api/articleManage";

export default {
  name: "ArticleAudit",
  components: {
    articleReader,
    articleList,
  },
  data() {
    return {
      // 待审核文章标题列表
      auditTitleList: [],
      // 待审核文章内容
      auditContent: "",
      pageInfo: {
        totalPage: "",
        pageNum: 0,
      },
      haveMore: false,
    };
  },
  methods: {
    async loadAuditList() {
      const APIData = (await getAuditTitleList(this.pageInfo.pageNum + 1)).data;
      console.log(APIData);
      if (APIData.articleList.length > 0) {
        this.auditTitleList = APIData.articleList;
        this.pageInfo.totalPage = APIData.pageInfo.totalPage;
        this.pageInfo.pageNum = 1;
      }
      //   控制是否还能继续加载
      if (this.pageInfo.totalPage > this.pageInfo.pageNum) {
        this.haveMore = true;
      } else {
        this.haveMore = false;
      }
      console.log(this.auditTitleList);
    },

    // 调取待审核文章内容
    async articleAudit(articleId) {
      const APIData = await getAUditArticle(articleId);
      console.log(APIData);
      if (APIData.code == 200) {
        this.auditContent = APIData.data;
        // 触发list上的全局事件以更新title的按钮组
        this.$bus.$emit("setItemStatus", articleId);
      } else {
        this.auditContent = "审核文章拉取失败";
      }
    },

    // 文章通过审核
    async articlePass(articleId) {
      // console.log(articleId);
      const APIData = await passAudit(articleId);
      console.log("文章审核通过....");
      if (APIData.code == 200) {
        this.auditTitleList = this.auditTitleList.filter(
          (title) => title.id != articleId
        );
        this.auditContent = "";
        // TODO:添加提示弹窗
      }
    },
    // 文章不通过审核
    async articleNoPass(articleId) {
      const APIData = await noPassAudit(articleId);
      if (APIData.code == 200) {
        this.auditTitleList = this.auditTitleList.filter(
          (title) => title.id != articleId
        );
        this.auditContent = "";
        // TODO:添加提示弹窗
      }
    },

    async showMore() {
      // 还是使用上一个搜索API，只是开始的页码变了
      const APIData = (await getAuditTitleList(this.pageInfo.pageNum + 1)).data;
      this.auditTitleList = this.auditTitleList.concat(APIData.articleList);
      //   当前页码加一
      this.pageInfo.pageNum += 1;
      if (this.pageInfo.totalPage > this.pageInfo.pageNum) {
        this.haveMore = true;
      } else {
        this.haveMore = false;
      }
    },
  },
  mounted() {
    // 加载待审核的文章标题列表
    this.loadAuditList();
    this.$bus.$on("articleAudit", this.articleAudit);
    this.$bus.$on("articlePass", this.articlePass);
    this.$bus.$on("articleNoPass", this.articleNoPass);
  },
};
</script>

<style scoped>
#container {
  height: 100%;
  background-color: #fff;
}

.el-row {
  height: 100%;
}
.el-row .el-col {
  height: 100%;
}
</style>